import { defineStore } from 'pinia';
import Vue from "./../main";

/**
 *  defineStore 第一个参数，实际项目中建议定义成枚举类型
 * 实际项目中，定义不同的store用不同的文件分割出来，index中用来到处store 和基础的数据 useStore 
 */

function getDict(params) {
	return new Promise(function(resolve, reject){
		Vue.http.get('global.getDict',{ params }).then(res => {
			resolve(res.data);
		}).catch(err => reject(err));
	})
}

function getHttp(url) {
	return new Promise(function(resolve, reject){
		Vue.http.get(url).then(res => {
			let arr = res.data.map(i => {
				i['label'] = i.name;
				i['value'] = i.id;
				return i;
			})
			resolve(arr);
		}).catch(err => reject(err));
	})
}

export const globalStore = defineStore("GLOBAL", {
	state: () => ({

		// 下拉数据
		hatSelect: [
			{label: 'Wearing A Hat', value: 'Wearing A Hat'},
			{label: 'Wearing A Helmet', value: 'Wearing A Helmet'},
			{label: 'Not Wearing A Hat Or Helmet', value: 'Not Wearing A Hat Or Helmet'},
		],		// hat 
		wearingGlassesSelect: [
			{label: 'Wearing Glasses', value: 'Wearing Glasses'},
			{label: 'Not Wearing Glasses', value: 'Not Wearing Glasses'},
		],		// wearing glasses 
		ageSelect: [
			{label: 'Child', value: 'Child'},
			{label: 'Young Adult', value: 'Young Adult'},
			{label: 'Middle-aged Adult', value: 'Middle-aged Adult'},
			{label: 'Elderly', value: 'Elderly'},
		],		// age 
		genderSelect: [
			{label: 'Male', value: 'Male'},
			{label: 'Female', value: 'Female'},
		],		// gender 
		backpackSelect: [
			{label: 'Wearing A Backpack', value: 'Wearing A Backpack'},
			{label: 'Not Wearing A Backpack', value: 'Not Wearing A Backpack'},
		],		// backpack 
		handbagSelect: [
			{label: 'Carrying A Handbag', value: 'Carrying A Handbag'},
			{label: 'Not Carrying A Handbag', value: 'Not Carrying A Handbag'},
		],		// handbag 

		algorithmSelect: [],		// 算法 
		locationSelect: [],		// 地址 
		stateSelect: [],		// 状态 
		cameraSelect: [],		// 摄像头 
		alarmSelect: [],		// 告警 
		ratioSelect: [],		// Ratio 
		pixelSelect: [],	// Pixel 

	}),
	getters: {
		// double: (state) => state.count * 2,
	},
	// 修改方法
	actions: {
		/**
		 * @param {String} name algorithmSelect	locationSelect	stateSelect	cameraSelect	alarmSelect
		 */
		async getSelect(name) {
			let select = [];
			if (this[name].length > 0) {
				return this[name]
			}else{
				let selectList = [
					{label: 'algorithmSelect', value: 'algorithm_type'},
					{label: 'locationSelect', value: 'camera_location'},
					{label: 'stateSelect', value: 'camera_state'},
					{label: 'cameraSelect', value: 'http', url: 'global.getCameraSelect'},
					{label: 'alarmSelect', value: 'alarm_name'},
					{label: 'ratioSelect', value: 'camera_ratio'},
					{label: 'pixelSelect', value: 'camera_pixel'},
				]
				let item = selectList.find(i => i.label == name);
				if (item?.value != 'http') {
					let params = {
						name: item ? item.value : ''
					};
					select = item ? await getDict(params) : [];
				}else{
					select = item ? await getHttp(item.url) : [];
				}
				this[name] = select;
				return select;
			}
		},
	},
})